﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Infrastructure;

using Microsoft.Extensions.Logging;

using ZR.Model.Models.ProductionControl;
using static System.Runtime.InteropServices.JavaScript.JSType;
using CKS.PLC.Action.Subscription.OP10;

namespace CKS.PLC.Action.OP10
{
    /// <summary>
    /// 
    /// 进清洗站 - 清洗线数量做增加 - 子篮数量做增加
    /// 
    /// </summary>
    public class Cks101Action : LoadingActionBase
    {
        public Cks101Action(DecodeValue _value) : base(_value)
        {
        }

        /*
         进清洗线， 增加清洗线零件数量，重置子篮零件数量
         */
        public override void Execute()
        {
            var replayDB = 3010;
            var replayAction = 101;
            var result = Value.Value.ToInt();
            var oldBasketStatus = -1;//子篮之前的状态
            var replayPos = Value.GetPosition(replayDB, replayAction);
            if (result == 1)
            {

                string bluecode = Value.GetLinkData(1).ToString();
              

                if (CheckCodeIsError(bluecode))
                {
                    AckWriteInt16(replayPos, 1);
                    EventBus.AddLog($"OP10-进清洗线  {bluecode}");
                    return;
                }
                /*
                 * 根据子篮查询零件类型
                 查询零件配置信息，做数量增加用
                 */

                var PartType = GetPartType(bluecode);


                var config = db.Queryable<CksLoadingplanConfig>()
                    .Where(a => a.PartType == PartType).First();
                // config.BasketCapacity
                config.LastLoadingTime = DateTime.Now;
                db.Updateable(config).UpdateColumns(a => new { a.LastLoadingTime }).ExecuteCommand();

                //更新子篮表的数量，如果子篮零件数量不是0，需要修正一下缓存线零件数量
                var basket = db.Queryable<CksBasket>().Where(a => a.BlueCode == bluecode).First();
                if (basket == null)
                {
                    basket = new CksBasket() { BlueCode = bluecode, CreatedTime = DateTime.Now, Type = PartType };
                    basket.Stock = config.BasketCapacity;
                    basket.Status = 1;
                    db.Insertable(basket).ExecuteCommand();
                }
                else
                {
                    oldBasketStatus = basket.Status;
                    //子篮增加状态：清洗线还是在缓存状态
                    basket.Status = 1;
                    basket.Stock = config.BasketCapacity;
                    basket.UpdateTime = DateTime.Now;
                    db.Updateable(basket).UpdateColumns(a => new { a.Stock, a.UpdateTime,a.Status }).ExecuteCommand();
                }
                //新增进清洗线记录
                CksBasketcleaningRecord record = new CksBasketcleaningRecord()
                {
                    BlueCode = bluecode,
                    CreatedTime = DateTime.Now,
                    Type = PartType,
                    InTime = DateTime.Now,
                    Status = 1
                };
                db.Insertable(record).ExecuteCommand();
                //进清洗线后更新上料计划表的信息
                UpdateLoadingPlan(oldBasketStatus, PartType);

                //更新当前零件清洗线\缓存线数量，查询子篮零件数量进行统计
                PushPartCount(PartType);

                
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP10-进清洗线 {bluecode} ");
            }
            else
            {
                
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog($"OP10-进清洗线 重置保存状态");
                
            }
        }
       

    }
}
